package biz.datalk.industrialland.common.component.filter;

import biz.datalk.commons.utils.ip.IPUtil;
import biz.datalk.industrialland.common.def.CommonConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @author tarofang@163.com
 * @date 2020-11-18
 */

public class DatalkCorsFilter extends CorsFilter {

    private static final Logger logger = LoggerFactory.getLogger(DatalkCorsFilter.class);

    public DatalkCorsFilter(CorsConfigurationSource configSource) {
        super(configSource);
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String reqUri = request.getRequestURI();
        // =========================== 测试用 S ==============================
        if (logger.isDebugEnabled()) {
            logger.debug("show debug start. uri: {}  ========================== S ", reqUri);
            String headerName, val;
            for (Enumeration<String> headerNames = request.getHeaderNames(); headerNames.hasMoreElements();) {
                headerName = headerNames.nextElement();
                for (Enumeration<String> headerValues = request.getHeaders(headerName); headerValues.hasMoreElements();) {
                    val = headerValues.nextElement();
                    logger.debug("uri: {}, head: {} <====> {}", reqUri, headerName, val);
                }
            }
            // Enumeration<String> headerNames = request.getHeaderNames();
            // String headerName;
            // while (headerNames.hasMoreElements()) {
            //     headerName = headerNames.nextElement();
            //     logger.debug("uri: {}, head: {} <====> {}", reqUri, headerName, request.getHeader(headerName));
            // }
            logger.debug("show debug start. uri: {}  ========================== E ", reqUri);
        }
        // =========================== 测试用 E ==============================
        if (!logger.isDebugEnabled() && logger.isInfoEnabled()) {
            String clientType = request.getHeader(CommonConstant.X_CLIENT_TYPE);
            String clientVer = request.getHeader(CommonConstant.X_CLIENT_VER);
            String token = request.getHeader(CommonConstant.ACCESS_TOKEN);
            String realIp = IPUtil.getIpAddress(request);
            String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
            String xForwardFor = request.getHeader("x-forwarded-for");
            // String xReaIp = request.getHeader("x-real-ip");
            String host = request.getHeader(HttpHeaders.HOST);
            String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);
            String method = request.getMethod();
            logger.info("RequestBriefInfo:[uri={}, method={}, ip={}, clientType={}, clientVer={}, token={}, userAgent={}, xForwardFor={}, host={}, contentType={}]", //
                    reqUri, method, realIp, clientType, clientVer, token, userAgent, xForwardFor, host, contentType);
        }
        super.doFilterInternal(request, response, filterChain);
        // String method = request.getMethod();
        // method = StringUtils.trim(method).toUpperCase();
        // if (StringUtils.equals(RequestMethod.OPTIONS.name(), method)) {
        //     filterChain.doFilter(request, response);
        // } else {
        //     super.doFilterInternal(request, response, filterChain);
        // }
    }
}
